package com.tj.test.sys.controller;

import com.tj.test.sys.po.Paper;
import com.tj.test.sys.service.IPaperService;
import com.tj.test.sys.tool.ResponseMessage;
import com.tj.test.sys.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
@RequestMapping("/Paper")
@Api(tags = "Paper")
public class PaperController {
    @Autowired
    private IPaperService service;

    @ApiOperation("根据条件查询所有试卷信息")
    @RequestMapping(value = "/selectAllPaper",method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage selectAllPaper(Paper paper){
        Map<String,List<Paper>> map=new HashMap<>();
        List<Paper> list=service.selectAllPaper(paper);
        if (!list.isEmpty()){
            map.put("AllPaper",list);
            return Result.success("查询成功",map);
        }
        return Result.error("查询失败",false);
    }

    @ApiOperation("插入试卷信息")
    @RequestMapping(value = "/insertPaper",method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage insertPaper(Paper paper){
        int temp=0;
        if (paper!=null){
            if (paper.getPaperid()!=0){
                temp=service.insertPaper(paper);
                if (temp>0) {return Result.success("插入试卷信息成功",temp);}
                else  {return Result.error("该试卷已存在",temp);}
            }
        }
        return Result.error("插入试卷信息失败",temp);
    }

    @ApiOperation("删除试卷信息")
    @RequestMapping(value = "/deletePaper",method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage deletePaper(Paper paper){
        int temp=0;
        if (paper!=null){
            if (paper.getPaperid()!=0){
                temp=service.deletePaper(paper);
                if (temp>0) {return Result.success("删除试卷信息成功",temp);}
                else {return Result.error("无该试卷",temp);}
            }
        }
        return Result.error("删除试卷信息失败",temp);
    }

    @ApiOperation("修改试卷信息")
    @RequestMapping(value = "/updatePaper",method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage updatePaper(Paper paper){
        int temp=0;
        if (paper!=null){
            if (paper.getPaperid()!=0){
                temp=service.updatePaper(paper);
                if (temp>0) {return Result.success("更新试卷信息成功",temp);}
                else {return Result.error("无该试卷表信息",temp);}
            }
        }
        return Result.error("更新试卷信息失败",temp);
    }
}
